package com.zjmzxfzhl.modules.purchase.controller;

import java.util.Arrays;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.modules.purchase.entity.PurchasePayment;
import com.zjmzxfzhl.modules.purchase.service.PurchasePaymentService;

/**
 * Controller
 * 
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/payment")
public class PurchasePaymentController extends BaseController {
	@Autowired
	private PurchasePaymentService purchasePaymentService;

	/**
	 * 自定义查询列表
	 * 
	 * @param purchasePayment
	 * @param current
	 * @param size
	 * @return
	 */
	@RequiresPermissions("purchase:payment:list")
	@GetMapping(value = "/list")
	public Result list(PurchasePayment purchasePayment, @RequestParam Integer current, @RequestParam Integer size) {
		IPage<PurchasePayment> pageList = purchasePaymentService.list(new Page<PurchasePayment>(current, size), purchasePayment);
		return Result.ok(pageList);
	}

	@RequiresPermissions("purchase:payment:list")
	@GetMapping(value = "/queryById")
	public Result queryById(@RequestParam String id) {
		PurchasePayment purchasePayment = purchasePaymentService.getById(id);
		return Result.ok(purchasePayment);
	}

	/**
	 * @功能：新增
	 * @param purchasePayment
	 * @return
	 */
	@RequiresPermissions("purchase:payment:save")
	@PostMapping(value = "/save")
	public Result save(@Valid @RequestBody PurchasePayment purchasePayment) {
		purchasePaymentService.save(purchasePayment);
		return Result.ok();
	}

	/**
	 * @功能：修改
	 * @param purchasePayment
	 * @return
	 */
	@RequiresPermissions("purchase:payment:update")
	@PutMapping(value = "/update")
	public Result update(@Valid @RequestBody PurchasePayment purchasePayment) {
		purchasePaymentService.updateById(purchasePayment);
		return Result.ok();
	}

	/**
	 * @功能：批量删除
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("purchase:payment:delete")
	@DeleteMapping(value = "/delete")
	public Result delete(@RequestParam String ids) {
		if (ids == null || ids.trim().length() == 0) {
			return Result.error("ids can't be empty");
		}
		String[] idsArr = ids.split(",");
		if (idsArr.length > 1) {
			purchasePaymentService.removeByIds(Arrays.asList(idsArr));
		} else {
			purchasePaymentService.removeById(idsArr[0]);
		}
		return Result.ok();
	}
}
